package com.mall.sun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.sun.model.dto.GoodDTO;
import com.mall.sun.model.dto.PageHelper;
import com.mall.sun.model.vo.ResultVO;
import com.mall.sun.service.GoodsService;
import com.mall.sun.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * description:  <br>
 * date: 2021/12/6 18:00 <br>
 * author: ztz <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/goods")
public class GoodController {
    @Autowired
    GoodsService goodsService;
    @PostMapping("/all")
    public ResultVO getAllGoods(@RequestBody PageHelper pageHelper){
        return CommonUtil.ajaxReturn(goodsService.getAllGood(pageHelper));
    }
    @GetMapping("/allTypes")
    public ResultVO getAllTypes(){
        return CommonUtil.ajaxReturn(goodsService.getAllTypes());
    }
    @PostMapping("/allAgents")
    public ResultVO getAllAgents(@RequestBody PageHelper pageHelper){
        return CommonUtil.ajaxReturn(goodsService.getGoodsAgent(pageHelper));
    }
    @PostMapping("/allSuppier")
    public ResultVO getAllSuppier(@RequestBody PageHelper pageHelper){
        return CommonUtil.ajaxReturn(goodsService.getGoodsSuppier(pageHelper));
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/delete")
    public ResultVO deleteOne(@RequestBody GoodDTO goodDTO){
        return CommonUtil.ajaxReturn(goodsService.removeById(goodDTO.getGId()));
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/delete/multi")
    public ResultVO deleteMulti(@RequestBody GoodDTO[] goodDTO){
        try {
            for (GoodDTO dto : goodDTO) {
                goodsService.removeById(dto.getGId());
            }
            return CommonUtil.ajaxReturn("删除成功");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return CommonUtil.ajaxReturn("删除失败");
    }
    @PutMapping("/update")
    public ResultVO updateOne(@RequestBody GoodDTO goodDTO){
        System.out.println(goodDTO);
        return CommonUtil.ajaxReturn(goodsService.updateGood(goodDTO));
    }
    @GetMapping("/count")
    public ResultVO countGoods(){
        return CommonUtil.ajaxReturn(goodsService.countGoods());
    }
    @GetMapping("/search/{gname}")
    public ResultVO searchGood(@PathVariable String gname){
        return CommonUtil.ajaxReturn(goodsService.search(gname));
    }
    @GetMapping("goodExp")
    public ResultVO getDayExp(){
        return CommonUtil.ajaxReturn(goodsService.getDayExp());
    }
    @GetMapping("expGood")
    public ResultVO getExpGood(){
        return CommonUtil.ajaxReturn(goodsService.getExpGood());
    }
}
